/* threadtest.c
 *	Simple program to test whether running a user program works.
 *	
 *	Just do a "syscall" that shuts down the OS.
 *
 * 	NOTE: for some reason, user programs with global data structures 
 *	sometimes haven't worked in the Nachos environment.  So be careful
 *	out there!  One option is to allocate data structures as 
 * 	automatics within a procedure, but if you do this, you have to
 *	be careful to allocate a big enough stack to hold the automatics!
 */

#include "syscall.h"
int lock1;
int condition1;


void test1_thread1() {
	Write("Thread 1 acquiring lock 1.\n", 28, ConsoleOutput);
	Acquire(lock1);
	Write("Thread 1 signaling condition 1.\n", 32, ConsoleOutput);
	Signal(condition1,lock1);
	Write("Thread 1 releasing lock 1.\n", 28, ConsoleOutput);
	Release(lock1);
	Exit(0);
}

void test1_thread2() {
	Write("Thread 2 acquiring lock 1.\n", 28, ConsoleOutput);
	Acquire(lock1);
	Write("Thread 2 waiting on condtion 1.\n", 33, ConsoleOutput);
	Wait(condition1,lock1);
	Write("Thread 2 awoken.\n", 18, ConsoleOutput);
	Write("Thread 2 releasing lock 1.\n", 28, ConsoleOutput);
	Release(lock1);
	Exit(0);
}


int
main()
{
	Write("Starting test Fork and Exec\n", 29, ConsoleOutput);
	
	lock1 = CreateLock("Lock1", 5);
	condition1 = CreateCondition("Condition1", 10);
	
	Fork(test1_thread2);
	Write("Forked thread 2.\n", 18, ConsoleOutput);
	
	Fork(test1_thread1);
	Write("Forked thread 1.\n", 18, ConsoleOutput);

	return 0;
    /* not reached */
}
